package com.ham.ui;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;

import org.javabuilders.swing.SwingJavaBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ham.dto.ItemDTO;
import com.ham.facade.ItemRatingReaderFacade;
import com.ham.facade.ItemReaderFacade;
import com.ham.facade.MainFacade;
import com.ham.facade.impl.ItemCSVReaderFacadeImpl;
import com.ham.facade.impl.ItemRatingCSVReaderFacadeImpl;
import com.ham.facade.impl.ItemRatingXLSReaderFacadeImpl;
import com.ham.facade.impl.ItemXLSReaderFacadeImpl;
import com.ham.facade.impl.MainFacadeImpl;
import com.ham.model.ItemModel;
import com.ham.model.ItemRatingModel;
import com.toedter.calendar.JDateChooser;

/**
 * 
 * @author dprima
 * 
 */
public class MainPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 9029485734204362981L;
	private static final Logger LOGGER = LoggerFactory
			.getLogger(MainPanel.class);

	private JComboBox cmbInput;
	private JDateChooser ratingDate;

	private JTextField txtFilePath;
	private JTextField txtFilePath2;

	private final String inputType[] = { "Database", "CSV", "MS Excel" };

	public MainPanel() {
		SwingJavaBuilder.build(this);

		cmbInput.setModel(new DefaultComboBoxModel(inputType));

		txtFilePath.setEditable(false);
		txtFilePath2.setEditable(false);
	}

	public void cancelAction() throws IOException, SQLException {
		ratingDate.setDate(null);
		cmbInput.setSelectedIndex(0);
		txtFilePath.setText("");
		txtFilePath2.setText("");
	}

	public void browseFile() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.showOpenDialog(this);

		txtFilePath.setText(fileChooser.getSelectedFile().getAbsolutePath());
	}

	public void browseFile2() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.showOpenDialog(this);

		txtFilePath2.setText(fileChooser.getSelectedFile().getAbsolutePath());
	}

	public void generateDBReport() throws IOException, SQLException {
		ItemReaderFacade facade;
		ItemRatingReaderFacade facade2;
		switch (cmbInput.getSelectedIndex()) {
		case 0:
			generateFromDB();
			break;
		case 1:
			facade = new ItemCSVReaderFacadeImpl();
			List<ItemModel> items = facade.getItemList(txtFilePath.getText());
			facade.importItems(items);

			facade2 = new ItemRatingCSVReaderFacadeImpl();
			List<ItemRatingModel> ratings = facade2.getItemList(txtFilePath2
					.getText());
			facade.importItemRatings(ratings);
			generatedFromDBTmp();
			break;
		case 2:
			facade = new ItemXLSReaderFacadeImpl();
			List<ItemModel> itemsXLS = facade
					.getItemList(txtFilePath.getText());
			facade.importItems(itemsXLS);

			facade2 = new ItemRatingXLSReaderFacadeImpl();
			List<ItemRatingModel> ratingsXLS = facade2.getItemList(txtFilePath2
					.getText());
			facade.importItemRatings(ratingsXLS);
			generatedFromDBTmp();
			break;
		default:
			break;
		}
	}

	private void generatedFromDBTmp() {
		try {
			MainFacade facade = new MainFacadeImpl();
			List<ItemDTO> items = facade
					.populateDataForReportFromFile(ratingDate.getDate());

			InputStream in = getClass().getResourceAsStream(
					"/compiledreports/MainReport.jasper");

			HashMap<String, Object> reportParams = new HashMap<String, Object>();
			reportParams.put("DATA_LIST", items);
			JasperPrint jp = JasperFillManager.fillReport(in, reportParams,
					new JRBeanCollectionDataSource(items));
			JasperViewer.viewReport(jp, false);
		} catch (JRException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void generateFromDB() {
		try {
			MainFacade facade = new MainFacadeImpl();
			List<ItemDTO> items = facade.populateDataForReport(ratingDate
					.getDate());

			InputStream in = getClass().getResourceAsStream(
					"/compiledreports/MainReport.jasper");

			HashMap<String, Object> reportParams = new HashMap<String, Object>();
			reportParams.put("DATA_LIST", items);
			JasperPrint jp = JasperFillManager.fillReport(in, reportParams,
					new JRBeanCollectionDataSource(items));
			JasperViewer.viewReport(jp, false);
		} catch (JRException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
